package com.bogu.activity;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;


import com.bogu.constant.ConstantS;
import com.example.boguplatform.R;


import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class AttachmentGridAdapter extends BaseAdapter{
	
	private Activity activity;
	private LayoutParams layoutParams;
	private GridView gridView;
	public ArrayList<String> attachments=new ArrayList<String>();
//	private ImageLoader imageLoader=ImageLoader.getInstance();
//	private DisplayImageOptions options;  
	private BitmapUtils bitmapUtils;
	private int padding;
	int w_h;
	
	public AttachmentGridAdapter(Activity activity, GridView gridView) {
		super();
		this.activity = activity;
		this.gridView = gridView;
		w_h=(activity.getWindowManager().getDefaultDisplay().getWidth()-
				dip2px(activity, 20))/4;
		padding=dip2px(activity, 5);
		layoutParams =new LayoutParams(w_h,w_h);
		ViewGroup.LayoutParams layoutParams_g=gridView.getLayoutParams();
		layoutParams_g.height=w_h*(attachments.size()/4+1)+dip2px(activity, 20);
		gridView.setLayoutParams(layoutParams_g);

		File diskCachePath=new File(ConstantS.diskCachePath);
		if(!diskCachePath.exists()){
			diskCachePath.mkdirs();
		}
//		  ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity)
//          .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
//          .discCache(new UnlimitedDiscCache(diskCachePath))
//          .build();
//		  imageLoader.init(config);
//		  
//		  
//		  options = new DisplayImageOptions.Builder()  
////	        .showImageOnLoading(R.drawable.iv_loading_image)       // 璁剧疆鍥剧墖涓嬭浇鏈熼棿鏄剧ず鐨勫浘锟�? 
////	        .showImageForEmptyUri(R.drawable.iv_image_bad)  // 璁剧疆鍥剧墖Uri涓虹┖鎴栨槸閿欒鐨勬椂鍊欐樉绀虹殑鍥剧墖  
////	        .showImageOnFail(R.drawable.iv_image_bad)       // 璁剧疆鍥剧墖鍔犺浇鎴栬В鐮佽繃绋嬩腑鍙戠敓閿欒鏄剧ず鐨勫浘锟�?     
////	        .cacheInMemory(true)                        // 璁剧疆涓嬭浇鐨勫浘鐗囨槸鍚︾紦瀛樺湪鍐呭瓨锟�? 
////	        .cacheOnDisc(true)                          // 璁剧疆涓嬭浇鐨勫浘鐗囨槸鍚︾紦瀛樺湪SD鍗′腑  
//////	        .displayer(new RoundedBitmapDisplayer(20))  // 璁剧疆鎴愬渾瑙掑浘锟�? 
//		  .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
//	        .build();                                   // 鍒涘缓閰嶇疆杩囧緱DisplayImageOption瀵硅薄  

		bitmapUtils = new BitmapUtils(activity);
	}
	
	public void reLayout(){
		ViewGroup.LayoutParams layoutParams_g=gridView.getLayoutParams();
		layoutParams_g.height=w_h*(attachments.size()/4+1)+dip2px(activity, 20);
		gridView.setLayoutParams(layoutParams_g);
		notifyDataSetChanged();
	}
	

	@Override
	public int getCount() {
		return attachments.size()+1;
	}

	@Override
	public Object getItem(int position) {
		if(position<attachments.size())
		    return attachments.get(position);
		else 
			return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨锟�?
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨锟�?
	
			convertView=getImageView();
		
		if(position==attachments.size()){
			((ImageView)convertView).setImageResource(R.drawable.button_bbs_attachment_item_add);
			((ImageView)convertView).setBackgroundDrawable(null);
		}else{
////			String url=mainEntries.get(position-1).getImage_url();
////	    	  if(url!=null&&!url.startsWith("http://")){
////	    		  int start;
////	    		  if((start=url.lastIndexOf("/"))!=-1)
////	    			  url=url.substring(start, url.length());
////	    		  url=ConstantS.URL_DOWN+ConstantS.DOWN_TYPE_QRCODE+"&file_name="+url;
////	    	  }
//			imageLoader.displayImage(url, (ImageView)convertView, options, null); 
//			imageLoader.displayImage(attachments.get(position), (ImageView)convertView, options);
//			((ImageView)convertView).setImageBitmap(BitmapFactory.decodeFile(attachments.get(position)));
//			bitmapUtils.display(((ImageView)convertView),attachments.get(position));
			bitmapUtils.display(((ImageView)convertView),attachments.get(position),new BitmapLoadCallBack<View>() {

				@Override
				public void onLoadCompleted(View arg0, String arg1,
						Bitmap arg2, BitmapDisplayConfig arg3,
						BitmapLoadFrom arg4) {
					ImageView imageView=((ImageView)arg0);
					imageView.setLayoutParams(layoutParams);
					imageView.setScaleType(ScaleType.CENTER_CROP);
//					int padding=CommunityRegActivity.dip2px(activity, 5);
					imageView.setPadding(padding,padding,padding,padding);
					imageView.setImageBitmap(arg2);
					imageView.setBackgroundResource(R.drawable.bg_border);
				}

				@Override
				public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
					// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨锟�?
					
				}
			} );
			
		}
		return convertView;
	}
	 public ImageView getImageView(){
			ImageView imageView=new ImageView(activity);
			imageView.setLayoutParams(layoutParams);
			imageView.setScaleType(ScaleType.FIT_CENTER);
			int padding=dip2px(activity, 5);
			imageView.setPadding(padding,padding,padding,padding);
			return imageView;
		}
	 /**
		 * 锟斤拷锟斤拷锟街伙拷锟侥分憋拷锟绞达拷 dp 锟侥碉拷位 转锟斤拷为 px(锟斤拷锟斤拷)
		 */
		public static int dip2px(Context context, float dpValue) {
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (dpValue * scale + 0.5f);
		}

		/**
		 * 锟斤拷锟斤拷锟街伙拷锟侥分憋拷锟绞达拷 px(锟斤拷锟斤拷) 锟侥碉拷位 转锟斤拷为 dp
		 */
		public static int px2dip(Context context, float pxValue) {
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (pxValue / scale + 0.5f);
		}

}
